'use strict';
var TocStruct = function(id, name, level){
    this._name = name;
    this._id = id;
    this._level = level;
    this._items = [];
    this._index = "";
    this.toHtml = function(){
        var html = "<li><a href=\"#"+this._id+"\">"+this._index+" "+this._name+"</a>\n";
        if(this._items.length>0){
            html += "<ul>\n";
            for(var i=0; i<this._items.length; i++){
                this._items[i].index(this._index+(i+1)+".");
                html += this._items[i].toHtml();
            }
            html += "</ul>\n";
        }
        html += "</li>\n";
        return html;
    };
    this.id = function(id){
        if(id!=null){
            this._id = id;
        }
        return this._id;
    };
    this.name = function(name){
        if(name!=null){
            this._name = name;
        }
        return this._name;
    };
    this.level = function(level){
        if(level!=null){
            this._level = level;
        }
        return this._level;
    };
    this.index = function(index){
        if(index!=null){
            this._index = index;
        }
        return this._index;
    };
    this.addItem = function(item){
        if(item!=null){
            this._items.push(item);
        }
        return this._items;
    };
};
window.showdownToc = {
      type: 'output',
      filter: function (text, converter, options) {
            var topItems=[], itemChain=[];
            $($.parseHTML("<div>"+text+"</div>")).find("h1,h2,h3,h4,h5,h6,h7").each(function(i,item){
                var $item = $(item);
                var level = parseInt(item.tagName.substring(1));
                var struct = new TocStruct($item.attr("id"), $item.text(), level);
                var parentToc = itemChain.length<1 ? null : itemChain[itemChain.length-1];
                if(parentToc==null){
                    topItems.push(struct);
                }
                while(parentToc!=null){
                    if(parentToc.level() < level){//如果小于，说明parentToc是当前目录的上级目录，否则表示parentToc无子节点。
                        parentToc.addItem(struct);
                        break;
                    }else{//parentToc无子节点，则把他从当前的itemChain移除。
                        if(itemChain.length==1){//说明这个是根节点了。
                            if(topItems.indexOf(parentToc)<0){
                                topItems.push(parentToc);
                            }
                        }
                        itemChain.splice(itemChain.length-1, 1);
                        parentToc = itemChain.length<1 ? null : itemChain[itemChain.length-1];//再拿上一级目录。
                    }
                }
                itemChain.push(struct);//把当前目录加入链表中，下一个目录再来匹配。
            });
            var html = "<ul>\n";
            for(var i=0; i<topItems.length; i++){
                topItems[i].index((i+1)+".");
                html += topItems[i].toHtml();
            }
            html += "</ul>\n";
            return text.indexOf('[toc]') > -1 ? text.replace('[toc]', html) : text;
      }
};
